<?php

class Spotlights extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
	}
	
	public function index()
	{
		
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_spotlights'),
							'version' => '1.0'
		);	

		// build up our header_css array
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('admin_spotlights'),
							'media' => 'all',
							'version' => '1.0'
		);	
			
			
		$this->load->model('db/languages', 'languages');
		
		$qLanguages = $this->languages->get();
		
		
		$this->load->model('db/sites', 'sites');
		
		if ($this->oSecGroup->site_id !== null)
		{
			$this->sites->site_id = $this->oSecGroup->site_id;
		}
		
		$aOptions = array(
					'order_by' => array('site_name' => 'ASC')
		);
		
		
		
		$qSites = $this->sites->get($aOptions);
		
		
		$this->aData['qSites'] = $qSites;
		$this->aData['qLanguages'] = $qLanguages;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/spotlights/index', $this->aData);
	}
	
	
	
	public function edit_spotlight()
	{
		$aUri = $this->uri->uri_to_assoc(2);
		$iUri = count($aUri);
		
		$iVersionId = 0; 
		
		if (array_key_exists('version_id', $aUri) === true)
		{
			$iVersionId = (int) $aUri['version_id']; 
		}
		
		

		$this->aData['iVersionId'] = $iVersionId;
		
		
		$this->load->model('db/views/spotlights_by_site', 'vw_spotlights_by_site');
		
		$this->vw_spotlights_by_site->version_id = $iVersionId;
		
		$qSpotlights = $this->vw_spotlights_by_site->get(array(), 1);
		
		if ($qSpotlights->num_rows() === 0)
		{
			$oSpotlight = $this->vw_spotlights_by_site->get_object();
		}
		else 
		{
			$oSpotlight = $qSpotlights->row(0);	
		}
		
		
		$this->aData['oSpotlight'] = $oSpotlight;
		
		
		$this->load->model('db/views/spotlight_images', 'vw_spotlight_images');
		
		$this->vw_spotlight_images->version_id = $iVersionId;
		
		$aOptions = array(
					'order_by' => array('image_pos' => 'ASC')
		);
		
		$qSpotlightImages = $this->vw_spotlight_images->get($aOptions);
		$iSpotlightImages = $qSpotlightImages->num_rows();
		
		$sSpotlightContent = '';
		
		if ($iSpotlightImages > 0)
		{
			$aOptions = array(
						'order_by' => array('text_pos' => 'ASC')
			);
	
			for ($i = 0; $i < $iSpotlightImages; $i++)
			{
				$this->load->model('db/views/spotlight_text', 'vw_spotlight_text');
				
				$this->vw_spotlight_text->image_id = $qSpotlightImages->row($i)->image_id;
				$qSpotlightText = $this->vw_spotlight_text->get($aOptions);
				$iSpotlightText = $qSpotlightText->num_rows();
				
				$aSpotlightText = array();
				
				for ($j = 0; $j < $iSpotlightText; $j++)
				{
					$aSpotlightText[$j] = $qSpotlightText->row($j);
				}
				
				$qSpotlightImages->row($i)->qSpotlightText = $qSpotlightText; 
				
				$this->aData['oSpotlightImages'] = $qSpotlightImages->row($i);
				$this->aData['aSpotlightText'] = $aSpotlightText;
				
				$sSpotlightContent .= $this->load->view('platform/'.$this->sPlatform.'/admin/spotlights/spotlight_image_template', $this->aData, true);
			}
			
			
		}
		$this->aData['sSpotlightContent'] = $sSpotlightContent;
		
		
		
		$this->aPageJs[] = "
					var oSpotlight = {
						version_id : ".$iVersionId.",
						banner_images : ".$qSpotlightImages->num_rows().",
						imageSel : null	
					};
		";		
		
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('jquery_ui_core', 'jquery_ui_draggable', 'jquery_ui_sortable', 'jquery_treeview', 'admin_edit_spotlight'),
							'version' => '1.0'
		);	
		
		
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('jquery_treeview', 'admin_edit_spotlight'),
							'media' => 'all',
							'version' => '1.0'
		);			
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/spotlights/edit_spotlight', $this->aData);
	}
	
}

?>